package com.xyy.saas.payment.cores.bo;

import com.xyy.saas.payment.cores.enums.YopTradeStatusEnum;
import lombok.Data;

import java.io.Serializable;

@Data
public class QueryResultBo implements Serializable {

    private static final long serialVersionUID = -5517966105004085490L;

    /**
     *  订单状态 ： 进行中
     */
    public static final String STATUS_PROCESSING = "PROCESSING";
    /**
     * 订单状态 ： 已完成
     */
    public static final String STATUS_FINISHED = "FINISHED";
    /**
     * 订单状态 ： 失败
     */
    public static final String STATUS_FAILED = "FAILED";
    /**
     * 订单状态 ： 已取消
     */
    public static final String STATUS_CANCELED = "CANCELED";
    /**
     * 富民状态-处理中
     */
    public static final String PROCESSING = "0";
    /**
     * 富民状态-处理成功
     */
    public static final String FINISHED = "1";
    /**
     * 富民状态-处理失败
     */
    public static final String FAILED = "2";

    /**
     * 状态
     */
    private String status;
    /**
     * 金额
     */
    private Integer amount;
    /**
     * 手续费
     */
    private Integer fee;
    /**
     * 成功时间 unix 时间
     */
    private Long successTime;
    /**
     * 支付平台退款单号
     */
    private String refundNo;
    /**
     * 支付平台支付单号
     */
    private String payNo;
    /**
     * 渠道支付单号
     */
    private String channelChargeNo;

    /**
     * 退款的错误码
     */
    private String failureCode;

    /**
     * 退款消息的描述。
     */
    private String failureMsg;

    private String merchantId;

    public static String convert(String strStatus) {
        if(STATUS_PROCESSING.equals(strStatus) ||
                YopTradeStatusEnum.REQUEST_RECEIVE.name().equals(strStatus))
        {
            return "0";
        }
        else if(STATUS_FINISHED.equals(strStatus) ||
                YopTradeStatusEnum.SUCCESS.name().equals(strStatus))
        {
            return "1";
        }
        else if(STATUS_FAILED.equals(strStatus) ||
                YopTradeStatusEnum.FAIL.name().equals(strStatus) ||
                YopTradeStatusEnum.FAILED.name().equals(strStatus))
        {
            return "2";
        }

        return null;
    }

    public static String convertStatus(String intStr) {
        switch(intStr) {
            case "0":
                return STATUS_PROCESSING;
            case "1":
                return STATUS_FINISHED;
            case "2":
                return STATUS_FAILED;
        }

        throw new UnsupportedOperationException("intStr=" + intStr);
    }

    public static String convertStatus(Byte status) {
        switch(status) {
            case 1:
            case 2:
                return STATUS_PROCESSING;
            case 3:
                return STATUS_FINISHED;
            case 4:
                return STATUS_FAILED;
        }

        throw new UnsupportedOperationException("status=" + status);
    }

    /**
     * 平安退款状态 -> 支付平台退款状态
     * 平安（0 已受理;1 交易成功 ;2 交易中; 3 用户支付中; 4 交易关闭; 9 已撤销）
     */
    public static String convertPingtanRefundStatus(String pinganStatus) {
        if(pinganStatus == null) {
            return null;
        }

        switch(pinganStatus) {
            case "0":
            case "2":
            case "3":
                return PROCESSING;
            case "1":
                return FINISHED;
            case "4":
            case "9":
                return FAILED;
        }

        throw new UnsupportedOperationException("status=" + pinganStatus);
    }

    /**
     * 平安分账状态 -> 支付平台状态
     * 0-成功；1-失败；3-处理中
     */
    public static String convertPingtanSettleStatus(String pinganStatus) {
        if(pinganStatus == null) {
            return null;
        }

        switch(pinganStatus) {
            case "0":
                return FINISHED;
            case "1":
                return FAILED;
            case "3":
                return PROCESSING;
        }

        throw new UnsupportedOperationException("status=" + pinganStatus);
    }

    /**
     * 平安见证交易状态 -> 支付平台状态
     * （0：成功，1：失败，2：待确认, 5：待处理，6：处理中）
     */
    public static String convertPingtanTradeStatus(String pinganStatus) {
        if(pinganStatus == null) {
            return null;
        }

        switch(pinganStatus) {
            case "0":
                return FINISHED;
            case "1":
                return FAILED;
            case "2":
            case "5":
            case "6":
                return PROCESSING;
        }

        throw new UnsupportedOperationException("status=" + pinganStatus);
    }

    /**
     * 构建对象
     */
    public static QueryResultBo build(String status, Long successTime, Integer amount) {
        QueryResultBo queryResultBo = new QueryResultBo();
        queryResultBo.setStatus(status);
        queryResultBo.setSuccessTime(successTime);
        queryResultBo.setFee(0);
        queryResultBo.setAmount(amount);
        return queryResultBo;
    }

    /**
     * 构建对象
     */
    public static QueryResultBo build(String status, String channelChargeNo, Integer amount) {
        QueryResultBo queryResultBo = new QueryResultBo();
        queryResultBo.setStatus(status);
        queryResultBo.setChannelChargeNo(channelChargeNo);
        queryResultBo.setAmount(amount);
        return queryResultBo;
    }

    /**
     * 构建对象
     */
    public static QueryResultBo build(String status, Long successTime, String channelChargeNo, Integer amount) {
        QueryResultBo queryResultBo = new QueryResultBo();
        queryResultBo.setStatus(status);
        queryResultBo.setSuccessTime(successTime);
        queryResultBo.setChannelChargeNo(channelChargeNo);
        queryResultBo.setAmount(amount);
        return queryResultBo;
    }
}
